#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <memory>
#include <sstream>

struct GroupInfo {
    std::string groupId;
    std::string groupName;
    std::string ownerId;
    std::unordered_set<std::string> members;
    std::vector<std::string> messages;
    
    GroupInfo(const std::string& id, const std::string& name, const std::string& owner)
        : groupId(id), groupName(name), ownerId(owner) {
        members.insert(owner);
    }
    
    bool isMember(const std::string& userId) const {
        return members.find(userId) != members.end();
    }
    
    bool addMember(const std::string& userId) {
        members.insert(userId);
        return true;
    }
    
    void showInfo() const {
        std::cout << "\n📊 群组信息:" << std::endl;
        std::cout << "  🏷️  群组名: " << groupName << std::endl;
        std::cout << "  🆔 群组ID: " << groupId << std::endl;
        std::cout << "  👑 群主: " << ownerId << std::endl;
        std::cout << "  👥 成员数: " << members.size() << std::endl;
        std::cout << "  👤 成员: ";
        for (const auto& member : members) {
            std::cout << member << " ";
        }
        std::cout << std::endl;
    }
};

class SimpleGroupChat {
private:
    std::unordered_map<std::string, bool> users;
    std::unordered_map<std::string, std::shared_ptr<GroupInfo>> groups;
    int groupCounter = 1000;
    
public:
    bool login(const std::string& username) {
        users[username] = true;
        std::cout << "✅ " << username << " 登录成功！" << std::endl;
        return true;
    }
    
    std::string createGroup(const std::string& groupName, const std::string& ownerId) {
        if (users.find(ownerId) == users.end()) {
            std::cout << "❌ 创建者未登录" << std::endl;
            return "";
        }
        
        std::string groupId = "group_" + std::to_string(groupCounter++);
        auto group = std::make_shared<GroupInfo>(groupId, groupName, ownerId);
        groups[groupId] = group;
        
        std::cout << "🏠 群组创建成功: " << groupName << " (ID: " << groupId << ")" << std::endl;
        return groupId;
    }
    
    bool joinGroup(const std::string& groupId, const std::string& userId) {
        if (users.find(userId) == users.end()) {
            std::cout << "❌ 用户未登录" << std::endl;
            return false;
        }
        
        auto it = groups.find(groupId);
        if (it == groups.end()) {
            std::cout << "❌ 群组不存在" << std::endl;
            return false;
        }
        
        it->second->addMember(userId);
        std::cout << "✅ " << userId << " 成功加入群组: " << it->second->groupName << std::endl;
        return true;
    }
    
    bool sendGroupMessage(const std::string& groupId, const std::string& fromUser, const std::string& content) {
        auto it = groups.find(groupId);
        if (it == groups.end()) {
            std::cout << "❌ 群组不存在" << std::endl;
            return false;
        }
        
        auto group = it->second;
        if (!group->isMember(fromUser)) {
            std::cout << "❌ 不是群成员，无法发送消息" << std::endl;
            return false;
        }
        
        std::string message = fromUser + ": " + content;
        group->messages.push_back(message);
        
        std::cout << "📤 群消息已发送: " << message << std::endl;
        std::cout << "📡 消息已广播给 " << (group->members.size() - 1) << " 个成员" << std::endl;
        return true;
    }
    
    void showGroup(const std::string& groupId) {
        auto it = groups.find(groupId);
        if (it != groups.end()) {
            it->second->showInfo();
            
            std::cout << "\n💬 群聊记录:" << std::endl;
            for (const auto& msg : it->second->messages) {
                std::cout << "  " << msg << std::endl;
            }
        } else {
            std::cout << "❌ 群组不存在" << std::endl;
        }
    }
    
    void showAllGroups() {
        std::cout << "\n🏠 所有群组:" << std::endl;
        for (const auto& pair : groups) {
            auto group = pair.second;
            std::cout << "  📱 " << group->groupName << " (" << group->groupId << ")" 
                     << " - 群主: " << group->ownerId << " - " << group->members.size() << " 人" << std::endl;
        }
    }
};

void runDemo() {
    std::cout << "\n🎬 群聊演示开始..." << std::endl;
    
    SimpleGroupChat chat;
    
    // 登录用户
    std::cout << "\n👥 步骤1: 用户登录" << std::endl;
    chat.login("Alice");
    chat.login("Bob");
    chat.login("Charlie");
    
    // 创建群组
    std::cout << "\n🏠 步骤2: 创建群组" << std::endl;
    std::string group1 = chat.createGroup("技术讨论群", "Alice");
    std::string group2 = chat.createGroup("游戏交流群", "Bob");
    
    // 加入群组
    std::cout << "\n👋 步骤3: 加入群组" << std::endl;
    chat.joinGroup(group1, "Bob");
    chat.joinGroup(group1, "Charlie");
    chat.joinGroup(group2, "Alice");
    
    // 发送群消息
    std::cout << "\n💬 步骤4: 群聊交流" << std::endl;
    chat.sendGroupMessage(group1, "Alice", "欢迎大家！");
    chat.sendGroupMessage(group1, "Bob", "谢谢群主！");
    chat.sendGroupMessage(group1, "Charlie", "大家好！");
    
    chat.sendGroupMessage(group2, "Bob", "游戏群建立了！");
    chat.sendGroupMessage(group2, "Alice", "一起玩游戏吧！");
    
    // 查看群组信息
    std::cout << "\n📊 步骤5: 查看群组信息" << std::endl;
    chat.showGroup(group1);
    chat.showGroup(group2);
    
    chat.showAllGroups();
    
    std::cout << "\n🎉 群聊演示完成！" << std::endl;
}

int main() {
    std::cout << "\n" << std::string(60, '=') << std::endl;
    std::cout << "    🏠 群聊发起演示 - 多人聊天体验" << std::endl;
    std::cout << std::string(60, '=') << std::endl;
    
    std::cout << "\n📋 群聊发起方式:" << std::endl;
    std::cout << "1. 登录用户账号" << std::endl;
    std::cout << "2. 创建群组 (指定群名和群主)" << std::endl;
    std::cout << "3. 邀请或让其他用户加入群组" << std::endl;
    std::cout << "4. 开始群聊交流" << std::endl;
    
    std::cout << "\n输入 'demo' 运行自动演示，或 'quit' 退出: ";
    std::string input;
    std::getline(std::cin, input);
    
    if (input == "demo") {
        runDemo();
    } else {
        std::cout << "👋 再见！" << std::endl;
    }
    
    return 0;
} 